<template>
  <div class="info-dialog">
    <el-dialog
      :visible.sync="visible"
      :title="title"
      :modal-append-to-body="true"
      :append-to-body="true"
      :destroy-on-close="false"
      :modal="true"
      :close-on-click-modal="false"
      top="10vh"
      width="550px"
      @closed="closed"
    >
      <el-form ref="formData" :model="formData" :rules="rules" style="padding: 20px;" >
        <el-form-item label-width="80px" label="角色名称" prop="name">
          <el-input v-model="formData.name" placeholder="角色名称" autocomplete="off" />
        </el-form-item>
        <el-form-item label-width="80px" label="角色标识" prop="code">
          <el-input v-model="formData.code" placeholder="角色标识" autocomplete="off" />
        </el-form-item>
        <el-form-item label-width="80px" label="序号" prop="orderNum">
          <el-input-number v-model="formData.orderNum" :min="2" :max="100000" label="备注" />
        </el-form-item>
        <el-form-item label-width="80px" label="备注" prop="desc">
          <el-input v-model="formData.desc" type="textarea" placeholder="备注信息(250字符内)." />
        </el-form-item>
      </el-form>
      <div style="text-align: right;" slot="footer" class="dialog-footer">
        <el-button @click="closed" :loading="loading">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    saveRole
  } from '@/api/system/role'
  export default {
    name: 'InfoDialog',
    components: {
    },
    props: {
      properties: {
        type: Object,
        default: () => ({})
      }
    },
    computed:{

    },
    watch:{
      properties: {
        handler: function(value) {
          this.visible = value.visible
          if (value.data) {
            this.formData.id = value.data.id || ''
            this.formData.name = value.data.name || ''
            this.formData.code = value.data.code || ''
            this.formData.desc = value.data.desc || ''
            this.formData.orderNum = value.data.orderNum || 2

          } else {
            this.formData.id = ''
            this.formData.name = ''
            this.formData.code = ''
            this.formData.desc = ''
            this.formData.orderNum = 2
          }
          if (value.type) {
            this.type = value.type
            if (value.type === '0') {
              this.title = '新增'
            }
            if (value.type === '1') {
              this.title = '编辑'
            }
          }
        },
        immediate: true,
        deep: false
      }
    },
    data(){
      return {
        visible:false,
        title:'',
        loading:false,
        formData: {
          id: '',
          code: '',
          name: '',
          desc: '',
          orderNum:2
        },
        rules: {
          name: [{
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '长度在 1 到 50 个字符',
            trigger: 'blur'
          }
          ],
          code: [{
            required: true,
            message: '请输入角色标识.',
            trigger: 'blur'
          },
          {
            min: 2,
            max: 50,
            message: '长度在 2 到 50 个字符',
            trigger: 'blur'
          }
          ],
          desc: [{
              min: 0,
              max: 250,
              message: '长度在 0 到 250 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods:{
      submitForm: function() {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            this.loading = true
            saveRole(this.formData).then(rsp => {
              this.$message({
                type: 'success',
                message:rsp.message
              })
              this.closed()
            }).catch(err => {
              console.log(err);
            })
            .finally(() => {
              this.loading = false
            })
          }
        })
      },
      closed: function() {
        this.$emit('closed', false)
      },
    }
  }

</script>

</script>

<style lang="scss">
</style>
